<?php 
/**
 * TomatoCMS
 * 
 * LICENSE
 *
 * This source file is subject to the GNU GENERAL PUBLIC LICENSE Version 2 
 * that is bundled with this package in the file LICENSE.txt.
 * It is also available through the world-wide-web at this URL:
 * http://www.gnu.org/licenses/gpl-2.0.txt
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to license@tomatocms.com so we can send you a copy immediately.
 * 
 * @copyright	Copyright (c) 2009-2010 TIG Corporation (http://www.tig.vn)
 * @license		http://www.gnu.org/licenses/gpl-2.0.txt GNU GENERAL PUBLIC LICENSE Version 2
 * @version 	$Id: list.phtml 4213 2010-08-02 03:35:10Z leha $
 * @since		2.0.0
 */
?>
<?php
$this->headTitle($this->translator('banner_list_page_title'));

$this->headLink()
	->appendStylesheet($this->APP_STATIC_SERVER . '/js/jquery.ui/themes/base/ui.core.css')
	->appendStylesheet($this->APP_STATIC_SERVER . '/js/jquery.ui/themes/base/ui.resizable.css')
	->appendStylesheet($this->APP_STATIC_SERVER . '/js/jquery.ui/themes/base/ui.dialog.css')
	->appendStylesheet($this->APP_STATIC_SERVER . '/js/jquery.ui/themes/base/ui.theme.css');

$this->headScript()
	->appendFile($this->APP_STATIC_SERVER . '/js/jquery.ui/ui.core.js')
	->appendFile($this->APP_STATIC_SERVER . '/js/jquery.ui/ui.draggable.js')
	->appendFile($this->APP_STATIC_SERVER . '/js/jquery.ui/ui.resizable.js')
	->appendFile($this->APP_STATIC_SERVER . '/js/jquery.ui/ui.dialog.js')
	->appendFile($this->APP_STATIC_SERVER . '/js/jquery.validate/jquery.validate.min.js');
?>

<div class="grid_12 t_a_ui_breadcrumb">
	<span><?php echo $this->translator('banner_list_breadcrumb'); ?></span>
</div>

<div class="grid_12 t_a_bottom">
	<div class="grid_9 alpha"><?php echo $this->translator('banner_list_guide'); ?></div>
	
	<div class="grid_3 omega">
		<div class="t_a_right">	
			<a href="<?php echo $this->url(array(), 'ad_banner_add'); ?>" class="t_a_ui_button_link"><span><?php echo $this->translator('banner_add_breadcrumb'); ?></span></a>
		</div>
	</div>
</div>

<!-- Search form -->
<form method="post" action="<?php echo $this->url(array(), 'ad_banner_list'); ?>" id="searchBannerForm">
<div class="grid_12">
	<div class="prefix_1 suffix_1">
		<div class="clearfix t_a_bottom">
			<div class="grid_5 alpha">
				<label><?php echo $this->translator('banner_list_search_keyword'); ?>:</label>
				<input type="text" style="width: 180px" name="keyword" value="<?php if (isset($this->exp['keyword'])) : ?><?php echo $this->exp['keyword']; ?><?php endif; ?>" />
			</div>
			
			<div class="grid_5 omega">
				<label><?php echo $this->translator('banner_list_search_id'); ?>:</label>
				<input type="text" style="width: 100px" name="bannerId" id="bannerId" value="<?php if (isset($this->exp['banner_id'])) : ?><?php echo $this->exp['banner_id']; ?><?php endif; ?>" />
			</div>
		</div>
	
		<hr />
		
		<div class="clearfix t_a_bottom">
			<div class="grid_5 alpha">
				<label><?php echo $this->translator('banner_list_search_page'); ?>:</label>
				<select name="page" style="width: 180px">
					<option value="">---</option>
					<?php for ($i = 0; $i < count($this->pages); $i++) : ?>
					<option value="<?php echo $this->pages[$i]->route; ?>"<?php if (isset($this->exp['route']) && $this->exp['route'] == $this->pages[$i]->route) : ?> selected="selected"<?php endif; ?>><?php echo $this->pages[$i]->title; ?></option>
					<?php endfor; ?>
				</select>
			</div>
			
			<div class="grid_5 omega">
				<label><?php echo $this->translator('banner_list_search_status'); ?>:</label>
				<select name="status" style="width: 180px">
					<option value=""<?php if (!isset($this->exp['status'])) : ?> selected="selected"<?php endif; ?>>---</option>
					<option value="active"<?php if (isset($this->exp['status']) && $this->exp['status'] == 'active') : ?> selected="selected"<?php endif; ?>><?php echo $this->translator('banner_list_status_activated'); ?></option>
					<option value="inactive"<?php if (isset($this->exp['status']) && $this->exp['status'] == 'inactive') : ?> selected="selected"<?php endif; ?>><?php echo $this->translator('banner_list_status_not_activated'); ?></option>
				</select>
			</div>
		</div>
	
		<hr />
		
		<div class="clearfix t_a_bottom">
			<label>&nbsp;</label>
			<button type="submit"><span><?php echo $this->translator('banner_list_search_button'); ?></span></button>
		</div>
	</div>
</div>
</form>

<!-- List of banners -->
<?php if ($this->numBanners == 0) : ?>
	<div class="grid_12 t_a_bottom"><?php echo $this->translator('banner_list_not_found'); ?></div>	
<?php else : ?>
	<div class="grid_12 t_a_ui_data_list_header">
		<div class="grid_7 alpha">
			<div><?php echo $this->translator('banner_name'); ?></div>
		</div>
		
		<div class="grid_2"><?php echo $this->translator('banner_status'); ?></div>
		
		<div class="grid_2"><?php echo $this->translator('banner_image_url'); ?></div>
		
		<div class="grid_1 omega"><?php echo $this->translator('banner_id'); ?></div>
	</div>

	<div class="grid_12 t_a_bottom">
		<?php foreach ($this->banners as $banner) : ?>
		<div id="bannerRow_<?php echo $banner->banner_id; ?>" class="clearfix t_a_ui_data_list_content">
			<div class="grid_7 alpha">
				<div>
					<?php echo $banner->name.'<br /> at: '.date('Y-m-d H:i:s',strtotime($banner->created_date)); ?>
				</div>
				
				<div class="t_a_top t_a_ui_helper_action">
					<?php if ($this->allow('edit')) : ?>
					<a href="<?php echo $this->url(array('banner_id' => $banner->banner_id), 'ad_banner_edit'); ?>/"><?php echo $this->translator('banner_list_action_edit'); ?></a> |
					<?php endif; ?>
					
					<?php if ($this->allow('delete')) : ?> 
					<a class="deleteAction" rel="<?php echo $banner->banner_id; ?>" href="javascript: void(0);"><?php echo $this->translator('banner_list_action_delete'); ?></a> |
					<?php endif; ?>
					
					<?php if ($this->allow('activate')) : ?>
					<a class="activateAction" rel="<?php echo $banner->banner_id; ?>_<?php echo $banner->status; ?>" href="javascript: void(0);"><?php if ($banner->status == 'active') : ?><?php echo $this->translator('banner_list_action_deactivate'); ?><?php else : ?><?php echo $this->translator('banner_list_action_activate'); ?><?php endif; ?></a>
					<?php endif; ?>
				</div>
			</div>
			
			<div class="grid_2" id="bannerStatus_<?php echo $banner->banner_id; ?>">
				<?php if ($banner->status == 'active') : ?>
					<?php echo $this->translator('banner_list_status_activated'); ?>
				<?php elseif ($banner->status == 'inactive') : ?>
					<?php echo $this->translator('banner_list_status_not_activated'); ?>
				<?php else : ?>
					&nbsp;
				<?php endif; ?>
			</div>
			
			<div class="grid_2">
				<?php if (null != $banner->image_url) : ?> 
					<?php if ('.swf' == substr($banner->image_url, strlen($banner->image_url) - 4, 4)) : ?> 
					<object width="100" height="50">
						<param name="movie" value="<?php echo $banner->image_url; ?>">
						<embed src="<?php echo $banner->image_url; ?>" width="100" height="50"></embed>
					</object>	
					<?php else: ?>
					<img src="<?php echo $banner->image_url; ?>" width="100" height="50" />
					<?php endif; ?>
				<?php else : ?>
					&nbsp;
				<?php endif; ?>
			</div>
			
			<div class="grid_1 omega"><?php echo $banner->banner_id; ?></div>
		</div>
		<?php endforeach; ?>
	</div>
	
	<div class="grid_12 t_a_bottom t_a_ui_paginator">
		<?php echo $this->paginator()->slide($this->paginator, $this->paginatorOptions); ?>
	</div>
<?php endif; ?>

<div id="messageDialog"></div>

<div id="confirmDialog"></div>

<script type="text/javascript">
$(document).ready(function() {
	<?php if ($this->allow('delete')) : ?>
	$('a.deleteAction').click(function() {
		var self = this;
		$('#confirmDialog').html('<?php echo addslashes($this->translator('banner_list_action_delete_confirm')); ?>')
			.dialog({
				title: '<?php echo addslashes($this->translator('common_dialog_confirm')); ?>',
				dialogClass: 't_a_ui_dialog',
				modal: true,
				closeOnEscape: false,
				resizable: false,
				open: function(event, ui) {
					$('.ui-dialog-titlebar-close').hide();
					$(this).parent().find('.ui-dialog-buttonpane button').wrapInner('<span/>');
				},
				buttons: {
					'<?php echo addslashes($this->translator('common_dialog_confirm_no')); ?>': function() {
						$(this).dialog('destroy');
					},
					'<?php echo addslashes($this->translator('common_dialog_confirm_yes')); ?>': function() {
						$(this).dialog('destroy');
						$(self).html('').addClass('t_a_ui_helper_loading');
						var id = $(self).attr('rel');
						$.ajaxq('ad_banner', {
							type: 'POST',
							url: '<?php echo $this->url(array(), 'ad_banner_delete'); ?>',
							data: { id: id },
							success: function(response) {
								if ('RESULT_OK' == response) {
									$('#bannerRow_' + id).fadeOut('slow');
									$('#messageDialog').html('<?php echo addslashes($this->translator('banner_list_deleted_success')); ?>')
										.dialog({
											title: '<?php echo addslashes($this->translator('common_dialog_message')); ?>',
											dialogClass: 't_a_ui_dialog',
											modal: true,
									    	show: { effect: 'fade' },
											open: function(event, ui) {
												setTimeout(function() {
													$('#' + event.target.id).dialog('option', 'hide', 'fade').dialog('destroy');
												}, 3000);
											}
										});
								}
							}
						});
					}
				}
			});
	});
	<?php endif; ?>
	
	<?php if ($this->allow('activate')) : ?>
	$('a.activateAction').click(function() {
		var self = this;
		var arr = $(this).attr('rel').split('_');
		$(self).html('').addClass('t_a_ui_helper_loading');
		$.ajaxq('ad_banner', {
			url: '<?php echo $this->url(array(), 'ad_banner_activate'); ?>',
			type: 'POST',
			data: { id: arr[0], status: arr[1] },
			success: function(response) {
				$(self).attr('rel', arr[0] + '_' + response);
				if (response == 'active') {
					$('#bannerStatus_' + arr[0]).html("<?php echo $this->translator('banner_list_status_activated'); ?>");
					$(self).html("<?php echo $this->translator('banner_list_action_deactivate'); ?>");
					$('#messageDialog').html('<?php echo addslashes($this->translator('banner_list_activated_success')); ?>')
						.dialog({
							title: '<?php echo addslashes($this->translator('common_dialog_message')); ?>',
							dialogClass: 't_a_ui_dialog',
							modal: true,
					    	show: { effect: 'fade' },
							open: function(event, ui) {
								setTimeout(function() {
									$('#' + event.target.id).dialog('option', 'hide', 'fade').dialog('destroy');
								}, 3000);
							}
						});
				} else {
					$('#bannerStatus_' + arr[0]).html("<?php echo $this->translator('banner_list_status_not_activated'); ?>");
					$(self).html("<?php echo $this->translator('banner_list_action_activate'); ?>");
					$('#messageDialog').html('<?php echo addslashes($this->translator('banner_list_deactivated_success')); ?>')
						.dialog({
							title: '<?php echo addslashes($this->translator('common_dialog_message')); ?>',
							dialogClass: 't_a_ui_dialog',
							modal: true,
					    	show: { effect: 'fade' },
							open: function(event, ui) {
								setTimeout(function() {
									$('#' + event.target.id).dialog('option', 'hide', 'fade').dialog('destroy');
								}, 3000);
							}
						});
				}
				$(self).removeClass('t_a_ui_helper_loading');
			}
		});
	});
	<?php endif; ?>

	$('#searchBannerForm').validate({
		rules: {		
			bannerId: {
				number: true
			}
	    },
	    messages: {
	    	bannerId: {
	        	number: "<?php echo $this->translator('banner_list_id_invalid'); ?>"
			}
		}
	});
});
</script>
